#ifndef PAR_SEED_HARVEST_H
#define PAR_SEED_HARVEST_H

//#include "err.h"

#include "stdafx.h"
using namespace std;
class Par_Seed_Harvest
	{
		double seed_date[101][4][9];
		vector <string> croptype;	//"winter","summer","inter","perennial"
		vector <string> val;	  
			//min_seed_mon
			//min_seed_day
			//mean_seed_mon
			//mean_seed_day
			//max_seed_mon
			//max_seed_day
			//harvest_mon
			//harvest_day
        unsigned int crop_numb;
		//[cropcode][type of crop][type of value]
		//[type of crop] 0 winter; 1 summer; 2 inter; 3 perennial
		void read_seed_date(const char *file)
		{
			FILE *in;
			unsigned int x,z,y;
			if ((in=fopen(file,"r"))==NULL)
			{
				err::err3 (" Par_Seed_Harvest::read_seed_date:Can not find -  ./par/crop_seed_date.par -");
			}
			fscanf(in,"%i",&crop_numb);

			//Initialisation of identifying string vectors;
			croptype.push_back("winter");
			croptype.push_back("summer");
			croptype.push_back("inter");
			croptype.push_back("perennial");
			val.push_back("min_seed_day");
			val.push_back("min_seed_mon");
			val.push_back("mean_seed_day");
			val.push_back("mean_seed_mon");
			val.push_back("max_seed_day");
			val.push_back("max_seed_mon");
			val.push_back("harvest_day");
			val.push_back("harvest_mon");

			for (x=0;x<crop_numb;x++)
			{
				for (y=0;y<croptype.size();y++)
				{
					for (z=0;z<val.size();z++)
					{
						fscanf(in,"%lf",&seed_date[x][y][z]);
					}
				}
			}
		}
	public:
		Par_Seed_Harvest()
			{
                                string file;
				/*croptype.push_back("winter");
				croptype.push_back("summer");
				croptype.push_back("inter");
				croptype.push_back("perennial");
				val.push_back("seed_min_day");
				val.push_back("seed_min_month");
				val.push_back("seed_mean_day");
				val.push_back("seed_mean_month");
				val.push_back("seed_max_day");
				val.push_back("seed_max_month");
				val.push_back("harvest_day");
				val.push_back("harvest_month");
                                */
			}
        int read_paras(string root,string name, string suffix)
        {
            string file;
             file=root+name+suffix;
             read_seed_date(file.c_str());
             return 0;
        }
		int Annual_time_line(int cropcode, int fallowcode,vector <double> &annual_crop_cover)
			{
                  // input is cropcode;
                  // output is annual_crop_cropcover a vector with 12 entries (one for each month) with either the cropcode
                  //if plant is grown or a code for fallow;

				 int mon=0;
				 int val;
                                 double valharvest;
				 val=(int)get_val(cropcode,"perennial", "seed_mean_day");
				 if (val>0){
					for (val=0;val<=11;val++){
						annual_crop_cover.push_back(cropcode);
					}
				 }
				 else{
					val=(int)get_val(cropcode,"winter", "seed_mean_month");
					if (val>0){
						valharvest= get_val(cropcode,"winter","harvest_month");
						for(mon=0;mon<12;mon++)
						{
							if (valharvest>val)err::err2("Par_date_default_seeding_harvest::Annual_time_line: wintercrop but harvestdate is bigger than seed date");
							if((mon<=valharvest)||(mon>=val)) annual_crop_cover.push_back(cropcode);
							else    annual_crop_cover.push_back(fallowcode);
						}
					}
					else{
						val=(int)get_val(cropcode,"summer","seed_mean_month");
						if(val>0){
                            valharvest= get_val(cropcode,"winter","harvest_month");
                            for(mon=0;mon<12;mon++)
                            {
                                if (valharvest<val)err::err2("Par_date_default_seeding_harvest::Annual_time_line: summercrop but harvestdate is smaller than seed date");
									if((mon<=valharvest)&&(mon>=val)) annual_crop_cover.push_back(cropcode);
									else annual_crop_cover.push_back(fallowcode);
                                }
                            }
						else{
							err::err2("Par_date_default_seeding_harvest::Annual_time_line: strange crop id");
						}
					}
				}
                                 return 0;
			}
		double get_val(int cropcode, string icroptype,string ival)
			{		//returns a double
					// cropcode is the int coding of the list
					// icroptype is a string which can be "winter,"summer","inter","perennial"
					// ival is a string which stands for 
                             
                 unsigned int croptype_index;
                 unsigned int val_index;
                 double myval;

                 croptype_index=distance(croptype.begin(), find(croptype.begin(),croptype.end(),icroptype));
                 if (croptype_index==croptype.size())err::err2("Par_date_default_seeding_harvest::get_val: croptype is ill defined: "+ icroptype);
                 val_index=distance(val.begin(),find(val.begin(),val.end(),ival));
                 if(val_index==val.size())err::err2("Par_date_default_seeding_harvest::get_val: The kind of output variable is ill defined: "+ival);
                 
                 myval=seed_date[cropcode][croptype_index][val_index];
                  return myval;
			}
	  double get_val_by_index(int cropcode, unsigned int croptype_index,string ival)
			{		//returns a double
					// cropcode is the int coding of the list
					// icroptype is a string which can be "winter,"summer","inter","perennial"
					// ival is a string which stands for 
                 unsigned int val_index;
                 double myval;
				 if (croptype_index<0)err::err2("Par_date_default_seeding_harvest::get_val: croptype_index is ill defined: ");
                 if (croptype_index>=croptype.size())err::err2("Par_date_default_seeding_harvest::get_val: croptype_index is ill defined: ");
                 val_index=distance(val.begin(),find(val.begin(),val.end(),ival));
                 if(val_index==val.size())err::err2("Par_date_default_seeding_harvest::get_val: The kind of output variable is ill defined: "+ival);
                 
                 myval=seed_date[cropcode][croptype_index][val_index];
                  return myval;
			}

	};
#endif